package com.dolaw.phoneslidebardemo.bean;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.dolaw.phoneslidebardemo.R;
import com.dolaw.phoneslidebardemo.utils.RoundImageView;

public class FriendAdapter extends RecyclerView.Adapter<FriendAdapter.ViewHolder> {
    List<User> users = new ArrayList<>();
    Context context;
    LayoutInflater inflater;

    public FriendAdapter(List<User> users, Context context) {
        this.users = users;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.friend_item, null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        final User user = users.get(position);
        holder.name.setText(user.getName());
        holder.header.setImageResource(user.getHeaderid());
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("提示：");
                builder.setMessage(user.toString());
                builder.setIcon(R.mipmap.ic_launcher);
                builder.setCancelable(true);            //点击对话框以外的区域是否让对话框消失
                //设置正面按钮
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(context, "你点击了确定", Toast.LENGTH_SHORT).show();
                        dialog.dismiss();
                    }
                });
                builder.show();

            }
        });

        String mark = user.getStart();
        if (position == getPosition(mark)){
            holder.letterlayout.setVisibility(View.VISIBLE);
            holder.letter.setText(user.getStart());
        }else {
            holder.letterlayout.setVisibility(View.GONE);
        }

        if (position!=getItemCount()-1&&user.getStart().equalsIgnoreCase(users.get(position+1).getStart())){
            holder.underline.setVisibility(View.VISIBLE);
        }else {
            holder.underline.setVisibility(View.GONE);
        }


    }

    private int getPosition(String mark) {
        for (int i=0;i<getItemCount();i++){
            if (users.get(i).getStart().equalsIgnoreCase(mark)){
                return i;
            }
        }
        return -1;
    }

    @Override
    public int getItemCount() {
        return users.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout letterlayout;
        TextView letter;
        TextView name;
        RoundImageView header;
        View view;
        View underline;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            view = itemView;
            underline = itemView.findViewById(R.id.underline);
            letterlayout = itemView.findViewById(R.id.letterlayout);
            letter = itemView.findViewById(R.id.letter);
            name = itemView.findViewById(R.id.name);
            header = itemView.findViewById(R.id.header);
        }
    }
}